package com.gg.hj.util;

import com.gg.common.mapper.NumberMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class INumber {
    @Resource
    private NumberMapper numberMapper;

    public String number(String startNumber) throws Exception {
        //查询库存最新流水号
        String s = numberMapper.queryNumber(startNumber);
        //获取日期
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMdd");
        String date = simpleDateFormat.format(new Date(System.currentTimeMillis()));
        //返回类型流水号
        String Number="";
        if(s==null){
            if("100".equals(startNumber)){
                Number=startNumber+"010101"+"000001";//六位流水号
            }else{
                Number=startNumber+date+"0001";//四位流水号
            }
        }else {
            int num = Integer.parseInt(s);
            if("100".equals(startNumber)){
                Number = startNumber+"010101"+String.format("%06d",num+1);
            }else{
                Number = startNumber+date+String.format("%04d",num+1);
            }
        }
        this.numberMapper.save(Number);//添加流水号
        return Number;
    }
}
